<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/9
 * Time: 11:29
 */

namespace App\HttpController\V1;

/**
 * 我的收藏
 * @package App\HttpController\V1
 */
class Collection extends Auth
{
    protected $param;   //参数
    protected $searchTime;   //查询条件时间
    protected $limit = 10;  //每页条数
    protected $offset = 0;  //查询起步点

    function onRequest( ?string $action ): ?bool
    {
        $returnRequest = parent::onRequest( $action );
        $this->param = $this->input();

//        if ( !Checking::keyIsExistence( 'page', $this->param ) ) {
//            $this->writeJson( -1, null, Checking::MESSAGE['keyIsExistence'] );
//            return false;
//        }
        if (isset($this->param['page'])){
            $this->offset = ( $this->param['page'] - 1 ) * $this->limit;
        }

        #判断前端是否传过来时间
        if ( isset( $this->param['time'] ) && !empty( $this->param['time'] ) ) {
            $days = date( 't', strtotime( $this->param['time'] ) );
            $startTime = $this->param['time'] . '-01';
            $endTime = $this->param['time'] . '-' . $days . ' 23:59:59';
        } else {
            $startTime = date( 'Y-m' ) . '-01';
            $days = date( 't', strtotime( $startTime ) );
            $endTime = date( 'Y-m' ) . '-' . $days . ' 23:59:59';
        }
        $this->searchTime = [$startTime, $endTime];

        return $returnRequest;
    }
    /**
     * 收藏页面
     * type 1 文章 2国学 3问问 4活动 5轩轩
     * page 1分页
     */
    public function lists(){
        $input=$this->input();
        try{
            if (!in_array($input['type'],[1,2,3,4,5])){
                return $this->writeJson(201,null,'参数异常');
            }
            $list=$this->db
                ->where('class_id',$input['type'])
                ->where('user_id',$this->user->id)
                ->orderBy('id','desc')
                ->get('users_collection',[$this->offset,$this->limit],'*');

            $snow=new Snow();
            $field=$snow->my_field($input['type']);
            foreach ($list as $k=>$v){
                $study= $this->db
                    ->where('id',$v['study_id'])
                    ->getOne('study','*');
//                unset($list[$k]['study']['id']);
                if (isset($study['image'])){
                    $list[$k]['image']=explode(';',$study['image']);
                    foreach ($list[$k]['image'] as $kk=>$vv){
                        $list[$k]['image'][$kk]=$this->video_url.$vv;
                    }
                }
                $list[$k]['id']=$v['study_id'];
                $list[$k]['class_id']=$v['class_id'];
                $list[$k]['study_title']=$study['study_title'];
                $list[$k]['user_id']=$study['user_id'];
                $list[$k]['favorite']=$study['favorite'];
                $list[$k]['browse']=$study['browse'];
                $list[$k]['introduce']=$study['introduce'];
                $list[$k]['comm_num']=$study['comm_num'];
                $list[$k]['city']=$study['city'];
                $list[$k]['share_text']=$study['share_text'];
                if (isset($study['heights'])){
                    $list[$k]['heights']=$study['heights'];
                }
                $list[$k]['like']=$study['like'];
                if (isset($study['vip_video'])){
                    $list[$k]['vip_video']=$this->video_url.$study['vip_video'];
                }
                if (isset($study['video_img'])){
                    $list[$k]['video_img']=$this->video_url.$study['video_img'];
                }
                if (isset($study['widths'])){
                    $list[$k]['widths']=$study['widths'];
                }
                $list[$k]['create_time']=$study['create_time'];

                if (isset($list[$k]['user_id'])){
                    $users_info=$this->db
                        ->where('id',$list[$k]['user_id'])
                        ->getOne('users','id,headimg,nickname');
                    $list[$k]['headimg']=$this->video_url.$users_info['headimg'];
                    $list[$k]['nickname']= $users_info['nickname'];
                }
                if (isset($list[$k]['study']['introduce'])){
                    $list[$k]['study']['introduce']= strip_tags($list[$k]['study']['introduce']);
                }
                if (in_array($input['type'],[3,5])){
                    $list[$k]['is_like']=$snow->likes($this->db,$list[$k]['id'],$this->user->id);
                    $list[$k]['is_collect']=1;
                    $list[$k]['is_follow']=$snow->follow($this->db,$list[$k]['user_id'],$this->user->id);
                }
            }

            return $this->writeJson(200,$list,'1');
        }catch (\Exception $exception){
            return $this->writeJson($exception->getCode(),$exception->getMessage());
        }
    }
}